package com.hyx.api.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import java.util.ArrayList;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.hyx.api.mapper.ModelMapper;
import com.hyx.api.domain.Model;
import com.hyx.api.domain.bo.ModelBo;
import com.hyx.api.domain.vo.ModelVo;
import com.hyx.api.service.IModelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;

/**
 * 机型Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-20
 */
@RequiredArgsConstructor
@Service
public class ModelServiceImpl implements IModelService
{
    private final ModelMapper modelMapper;

    /**
     * 查询机型
     *
     * @param id 机型主键
     * @return 机型
     */
    @Override
    public ModelVo selectModelById(Long id)
    {
        //TODO 表以外的字段，写入vo
        return modelMapper.selectVoById(id);
    }

    /**
     * 查询机型列表
     *
     * @param bo 机型
     * @return 机型
     */
    @Override
    public List<ModelVo> selectModelList(ModelBo bo)
    {
        LambdaQueryWrapper<Model> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtils.isNotEmpty(bo.getName()), Model::getName, bo.getName());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getSystemType()), Model::getSystemType, bo.getSystemType());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStatus()), Model::getStatus, bo.getStatus());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getBrand()), Model::getBrand, bo.getBrand());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getModelType()), Model::getModelType, bo.getModelType());
        lqw.eq(Model::getDelFlag, 0);
        lqw.orderByAsc(Model::getSort);
        return modelMapper.selectVoList(lqw);
    }

    /**
     * 新增机型
     *
     * @param bo 机型
     * @return 结果
     */
    @Override
    public int insertModel(ModelBo bo)
    {
        Model model = tranEntityBeforeSave(bo);
        validEntityBeforeSave(model);
        return modelMapper.insert(model);
    }

    /**
     * 修改机型
     *
     * @param bo 机型
     * @return 结果
     */
    @Override
    public int updateModel(ModelBo bo)
    {
        Model model = tranEntityBeforeSave(bo);
        return modelMapper.updateById(model);
    }

    /**
     * 批量删除机型
     *
     * @param ids 需要删除的机型主键
     * @return 结果
     */
    @Override
    public int deleteModelByIds(Long[] ids)
    {
        List<Model> list = new ArrayList<>();
        for (Long id : ids){
            Model model = new Model();
            model.setDelFlag(2);
            model.setId(id);
            list.add(model);
        }
        boolean bool = modelMapper.updateBatchById(list);
        return bool ? 1 : 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Model entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     *   保存前的数据转换
     */
    private Model tranEntityBeforeSave(ModelBo bo) {
        //TODO 做一些数据转换
        if(ObjectUtils.isEmpty(bo.getStatus()))
            bo.setStatus(0);
        Model model = new Model();
        BeanUtils.copyProperties(bo, model);
        return model;
    }
}
